package com.party.controller.user.learning;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.*;
import com.party.service.news.NewsService;
import com.party.service.study.CurriculumService;
import com.party.service.study.LearningContentService;
import com.party.service.study.User2ContentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-8
 * Time: 下午10:18
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/learning")
public class LearningController {


    @Resource
    CurriculumService curriculumService;
    @Resource
    LearningContentService learningContentService;
    @Resource
    User2ContentService user2ContentService;
    private int pageSize = 18;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "curriculumList")
    public ModelAndView index(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        List<Curriculum> hasCurrList = new ArrayList<Curriculum>();//已经完成了的课程
        List<Curriculum> notCurrList = new ArrayList<Curriculum>();//还未完成的课程
        if ( session.getAttribute("sysUser") != null) {
            int uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            List<Curriculum> currList = curriculumService.getByUid(uid);
            //统计课程 内容的学习情况，总共的内容数和已经学习完的内容数
            for(Curriculum c:currList)
            {
                List<LearningContent> contentList =  learningContentService.getByCurrId(c.getId());
                c.setContentNum(contentList.size());//总共的内容数
                List<Integer> list = new ArrayList<Integer>();
                for(LearningContent l:contentList)
                {
                    list.add(l.getId());
                }
                User2Content user2Content = new User2Content();
                user2Content.setUid(uid);
                List<User2Content> ucList = user2ContentService.get(uid,list);
                Integer finishNum = 0;
                for (User2Content content :ucList) {
                    if(content.getIsfinished() != null && content.getIsfinished().equals("1"))
                    {
                        finishNum ++ ;
                    }
                }
                c.setFinishContentNum(finishNum);//已经学习完的内容数

                //将课程分为已完成和未完成
                if(c.getIsfinished() != null &&c.getIsfinished().equals("1"))
                {
                    hasCurrList.add(c);
                }
                else
                {
                    notCurrList.add(c);
                }
            }
        }
        else //未登陆。跳转到登陆页面
        {
            modelAndView.setViewName("/login");
            return modelAndView;
        }

        modelAndView.addObject("hasCurrList",hasCurrList);
        modelAndView.addObject("notCurrList",notCurrList);
        modelAndView.setViewName("user/learning/curriculumList");
        return modelAndView;
    }

    @RequestMapping(value = "/getContentByCurr")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req,HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        String currId = req.getParameter("currId");
        List<LearningContent> contentList = new ArrayList<LearningContent>();
        if ( session.getAttribute("sysUser") != null && currId != null && !"".equals(currId)) {
            int uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            int id = Integer.parseInt(currId);
            contentList =  learningContentService.getByCurrId(id);
            List<LearningContent> userContentList = learningContentService.getByUid(uid);
            if(userContentList != null && userContentList.size() >0)
            {
                //设置一些用户已经学过内容的属性
                for(LearningContent lc :contentList)
                {
                    for(LearningContent ulc :userContentList)
                    {
                        if(lc.getId() == ulc.getId())
                        {
                            lc.setRealtime(ulc.getRealtime());
                            lc.setIsFinish(ulc.getIsFinish());
                            break;
                        }
                    }
                }
            }
        }
        this.clearContent(contentList);
        map.put("list",contentList);
        return map;
    }

    @RequestMapping(value = "/startLearning")
    public ModelAndView startLearning(HttpServletRequest req,HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        String idStr = req.getParameter("id");//内容id
        LearningContent content = new LearningContent();
        int uid = 0;
        if ( session.getAttribute("sysUser") != null && idStr != null && !"".equals(idStr)&&Tools.isNumber(idStr)) {
            uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            int contentId = Integer.parseInt(idStr);
            content = this.learningContentService.getByIdUid(contentId, uid);
            if(content != null && content.getContent() != null && content.getContent().length > 0)
            {
                content.setContentStr(new String(content.getContent()));
            }
        }

        modelAndView.addObject("content",content);
        modelAndView.addObject("uid",uid);
        modelAndView.setViewName("user/learning/learning");
        return modelAndView;
    }

    @RequestMapping(value = "/saveLearningTime")
    @ResponseBody
    public Map<String,Object> saveLearningTime(HttpServletRequest req,HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        String uidStr = req.getParameter("uid");
        String realtimeStr = req.getParameter("realtime");
        String contentIdStr = req.getParameter("contentId");
        if(Tools.isNumber(uidStr) && Tools.isNumber(realtimeStr) && Tools.isNumber(contentIdStr))
        {
            Integer uid = Integer.parseInt(uidStr);
            Integer realtime = Integer.parseInt(realtimeStr);
            Integer contentId = Integer.parseInt(contentIdStr);

            int num = this.user2ContentService.updateTime(uid,contentId,realtime);
        }

        return map;
    }
    //清除掉学习内容(learningContent)的content属性，因为这个属性值太大，传到前端数据太大
    private void clearContent(List<LearningContent> list)
    {
        if(list!=null && list.size() >0)
        {
            for(LearningContent content:list)
            {
                content.setContent("".getBytes());
            }
        }
    }
}
